//
//  PaletteViewController.m
//  Test
//
//  Created by 杨德龙 on 15/11/29.
//  Copyright © 2015年 ___杨德龙___. All rights reserved.
//

#import "PaletteViewController.h"
#import "CoordinatingController.h"
#import "SetStrokeColorCommand.h"   //  设置 color 的command
#import "SetStrokeSizeCommand.h"    // 设置size的 command
#import "CommandSlider.h"


@interface PaletteViewController ()<SetStrokeSizeCommandDelegate,SetStrokeColorCommandDelegate>

@property (nonatomic,strong)SetStrokeSizeCommand *setStrokeSizeCommand;


@property (nonatomic,strong)SetStrokeColorCommand *setStrokeColorCommand;

@end

@implementation PaletteViewController
{
    
    __weak IBOutlet CommandSlider *redSlider_;
    
    __weak IBOutlet CommandSlider *greenSlider;
    
    __weak IBOutlet CommandSlider *blueSlider_;
    
    __weak IBOutlet CommandSlider *sizeSlider_;
    
    __weak IBOutlet UIView *plateView_;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置命令 和 代理
    [self initCommands];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**
 初始化 commands
 */
- (void)initCommands
{
    self.setStrokeSizeCommand = [[SetStrokeSizeCommand alloc] init];
    self.setStrokeSizeCommand.delegate = self;
    
    self.setStrokeColorCommand = [[SetStrokeColorCommand alloc] init];
    self.setStrokeColorCommand.delegate = self;
    
    redSlider_.command = self.setStrokeColorCommand;
    greenSlider.command = self.setStrokeColorCommand;
    blueSlider_.command = self.setStrokeColorCommand;
    
    sizeSlider_.command = self.setStrokeSizeCommand;
    
    
    SetStrokeColorCommand *colorCommand = self.setStrokeColorCommand;
    
    // set each color component provider
    // to the color command
    [colorCommand setRGBValuesProvider: ^(CGFloat *red, CGFloat *green, CGFloat *blue)
     {
         *red = [redSlider_ value];
         *green = [greenSlider value];
         *blue = [blueSlider_ value];
     }];
    
    // set a post-update provider to the command
    // for any callback after a new color is set
    [colorCommand setPostColorUpdateProvider: ^(UIColor *color)
     {
         [plateView_ setBackgroundColor:color];
     }];
    
    
    // restore the original values of the sliders
    // and the color of the small palette view
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    CGFloat redValue = [userDefaults floatForKey:@"red"];
    CGFloat greenValue = [userDefaults floatForKey:@"green"];
    CGFloat blueValue = [userDefaults floatForKey:@"blue"];
    CGFloat sizeValue = [userDefaults floatForKey:@"size"];
    
    [redSlider_ setValue:redValue];
    [greenSlider setValue:greenValue];
    [blueSlider_ setValue:blueValue];
    [sizeSlider_ setValue:sizeValue];
    
    UIColor *color = [UIColor colorWithRed:redValue
                                     green:greenValue
                                      blue:blueValue
                                     alpha:1.0];
    
    [plateView_ setBackgroundColor:color];
    
}

#pragma mark ---- SetStokeSizeColorCommand Delegate
- (void)command:(SetStrokeSizeCommand *)command didRequestForStrokeSize:(CGFloat *)size
{
    *size = [sizeSlider_ value];
}

- (void)command:(SetStrokeColorCommand *)command didFinishColorUpdateWithColor:(UIColor *)color
{
    [plateView_ setBackgroundColor:color];
}

- (void)command:(SetStrokeColorCommand *)command didRequestColorComponetsForRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue
{
    *red = [redSlider_ value];
    *green = [greenSlider value];
    *blue = [blueSlider_ value];
}

#pragma mark ---- 点击 Done 按钮 返回
- (IBAction)onDoneClick:(id)sender
{
    [[CoordinatingController sharedInstance] requestViewChangeByObject:sender];
}

#pragma mark ---- Slider Value Change

- (IBAction)onCommadSliderValueChange:(CommandSlider *)sender
{
    if (sender == sizeSlider_)
    {
        [self.setStrokeSizeCommand execute];
    }else{
        [self.setStrokeColorCommand execute];
    }
}





@end
